package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrdersMapper ordersMapper;
    /**
     * 营业额统计接口
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();
        list.add(begin);
        while (!begin.equals(end)){
           begin= begin.plusDays(1);
           list.add(begin);
        }

        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate date : list) {
            //取单天的零点零分
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            //取单天的23店59分59秒
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map = new HashMap<>();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);
           Double sum = ordersMapper.getByMap(map);
           sum = sum==null? 0.0 : sum;
           turnoverList.add(sum);
        }
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();
        turnoverReportVO.setDateList(StringUtils.join(list,","));
        turnoverReportVO.setTurnoverList(StringUtils.join(turnoverList,","));

        return turnoverReportVO ;
    }

    /**
     * 用户统计接口
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
           begin= begin.plusDays(1);
           dateList.add(begin);
        }
        List<Integer> totalUserList = new ArrayList<>();
        List<Integer> newUserList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map = new HashMap<>();
            map.put("end",endTime);
            totalUserList.add(ordersMapper.userCount(map));
            map.put("begin",beginTime);
            newUserList.add(ordersMapper.userCount(map));
        }

        UserReportVO userReportVO = new UserReportVO();
        userReportVO.setDateList(StringUtils.join(dateList,","));
        userReportVO.setTotalUserList(StringUtils.join(totalUserList, ","));
        userReportVO.setNewUserList(StringUtils.join(newUserList,","));
        return userReportVO;
    }



    /**
     * 订单统计接口
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin= begin.plusDays(1);
            dateList.add(begin);
        }
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        Map map = new HashMap<>();
        map.put("begin",beginTime);
        map.put("end",endTime);

        //订单总数
        Integer totalOrderCount= ordersMapper.ordersStatistics(map);
        map.put("status",Orders.COMPLETED);

        //有效订单数
        Integer validOrderCount = ordersMapper.ordersStatistics(map);

        //订单完成率
        Double orderCompletionRate =   totalOrderCount==0? 0 : validOrderCount+0.0/totalOrderCount;

        //存放每日订单书的集合
        List<Integer> orderCountList= new ArrayList<>();
        //存放每日有效订单书的集合
        List<Integer>  validOrderCountList= new ArrayList<>();
        for (LocalDate localDate : dateList) {
             beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
             endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            map.put("begin",beginTime);
            map.put("end",endTime);
            //每日订单总数

            orderCountList.add(ordersMapper.ordersStatistics(map));
            map.put("status",Orders.COMPLETED);

            //每日有效订单数

            validOrderCountList.add(ordersMapper.ordersStatistics(map));
        }
        OrderReportVO orderReportVO = new OrderReportVO();
        orderReportVO.setDateList(StringUtils.join(dateList,","));
        orderReportVO.setOrderCompletionRate(orderCompletionRate);
        orderReportVO.setTotalOrderCount(totalOrderCount);
        orderReportVO.setValidOrderCount(validOrderCount);
        orderReportVO.setOrderCountList(StringUtils.join(orderCountList,","));
        orderReportVO.setValidOrderCountList(StringUtils.join(validOrderCountList,","));
        return orderReportVO;
    }


    /**
     * 查询销量排名top10接口
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        //获得商品销量排名
       List<GoodsSalesDTO> goodsSalesDTOS= ordersMapper.getGoodsTop10(begin,end);

        List<String> names = goodsSalesDTOS.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numbers = goodsSalesDTOS.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());

        SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO();
        salesTop10ReportVO.setNameList(StringUtils.join(names,","));
        salesTop10ReportVO.setNumberList(StringUtils.join(numbers,","));
        return salesTop10ReportVO;
    }
}
